//
//Copyright 2002-2004 Stottler Henke Associates, Inc.
//All Rights Reserved.
//////////////////////////////////////////////////////////////////////

package com.stottlerhenke.simbionic.engine.parser;

import com.stottlerhenke.simbionic.api.*;
import com.stottlerhenke.simbionic.common.parser.*;
import com.stottlerhenke.simbionic.engine.SB_SingletonBook;
import com.stottlerhenke.simbionic.engine.core.*;
import java.util.ArrayList;

/**
 * This class implements the SB_ParserAppInterface for the engine.
 * This interface creates evaluable expression trees.
 */
public class SB_ParserEngineInterface extends SB_ParserAppInterface
{
	private SB_SingletonBook _book;
	
	public SB_ParserEngineInterface(SB_SingletonBook book) 
	{
		_book = book;
  }
	
  public SB_ExpressionNode CreateErrorNode( String msg, String badName/*=""*/)
  {
	    	String error = new String(this.GetCompleteExpression() + ": " + msg);

          return new SB_ExpError(error);
  }

  public SB_ExpressionNode CreateConstantNode(float value)
  {
          return new SB_ExpConstant(value);
  }

  public SB_ExpressionNode CreateConstantNode(int value)
  {
          return new SB_ExpConstant(value);
  }

  public SB_ExpressionNode CreateConstantNode(boolean value)
  {
          return new SB_ExpConstant(value);
  }

  public SB_ExpressionNode CreateConstantNode( String value)
  {
          return new SB_ExpConstant(value);
  }
  public SB_ExpressionNode CreateConstantNode( String value, boolean symbol)
  {
          return CreateConstantNode(value);
  }

  public SB_ExpressionNode CreateConstantNode(double x,double y,double z)
  {
          return new SB_ExpConstant((float)x,(float)y,(float)z);
  }

  public SB_ExpressionNode CreateArrayNode(ArrayList array)
  {
          return new SB_ExpArray(array);
  }

  public SB_ExpressionNode CreateClassNode(String className)
  {    
  	if (_book.getUserClassMap().isDefinedSBClass(className))
  		return new SB_ExpClass(className);
  	else
  		// identifier is not a class name
  		return null;
  }
  
  public SB_ExpressionNode CreateVariableNode( String varName)
  {
          return new SB_ExpVariable(varName);
  }

  public SB_ExpressionNode CreateFunctionNode(String funcName)
  {
    try{
      // retrieve the function object matching this function name
      SB_Function theFun = _book.getBehaviorRegistry().getFunction(funcName);

      return new SB_ExpFunction(theFun);
    }catch(SB_Exception e){
      return null;
    }
  }

  public SB_ExpressionNode CreateMethodNode(SB_ExpressionNode instance, String methodName)
  {
  	return new SB_ExpMethod(methodName);
  }
  
  public void AddArgument(SB_ExpressionNode func,SB_ExpressionNode arg)
  {
          SB_ExpFunction funcNode = (SB_ExpFunction)(func);
          funcNode.AddArgument( (SB_Expression)( arg ) );
  }

  public SB_ExpressionNode CreateTernaryOpNode(SB_ExpressionNode arg1, SB_ExpressionNode arg2, SB_ExpressionNode arg3,
                                              ETernaryOp op)
  {
      return new SB_ExpTernaryOp(arg1,arg2,arg3,op);
  }

  public SB_ExpressionNode CreateBinaryOpNode(SB_ExpressionNode arg1, SB_ExpressionNode arg2,
                                                                                                                                    EBinaryOp op)
  {
          return new SB_ExpBinaryOp(arg1,arg2,op);
  }

  public SB_ExpressionNode CreateUnaryOpNode(SB_ExpressionNode arg,
                                                                                                                                   EUnaryOp op)
  {
          return new SB_ExpUnaryOp(arg,op);
  }

  public EParseResult FinishFunction(SB_ObjectRefWrapper funcWrapper) 
  { 
  	return EParseResult.kMatch; 
  }
}